<template>
  <div class="order-view">
    <el-dialog
      :title=title
      :visible.sync="addShow"
      width="60%"
      v-if="addShow"
      :before-close="handleClose"
      :modal-append-to-body="false"
      :append-to-body="true"
    >
      <div>
        <el-form ref="form" :model="form" label-width="150px" label-position="left">
          <el-form-item label="盘点单编号" prop="takingCode">
            <el-input v-model="form.takingCode" disabled placeholder="请输入盘点单编号"/>
          </el-form-item>
          <el-form-item label="选择仓库" prop="houseName">
            <el-input :disabled="isUpd" v-model="form.houseName" @focus="selHouse" placeholder="请选择仓库"/>
          </el-form-item>
          <el-form-item label="选择商品分类" prop="typeName">
            <el-input :disabled="isUpd" v-model="form.typeName" @focus="selType" placeholder="请选择商品分类"/>
          </el-form-item>
          <el-button :disabled="isUpd" @click="confirmSel">确认</el-button>
        </el-form>
      </div>
      <!--        detail表格-->
      <el-table :data="stockTakingDetailList" style="width: 100%">
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="proName"/>
        <el-table-column label="实存数量" align="center" prop="realNum">
          <template slot-scope="scope">
            <el-input v-model="scope.row.realNum"
                      @change="changeRealNum(scope.row)"
                      :disabled="!isUpd">
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="库存数量" align="center" prop="stock">
        </el-table-column>
        <el-table-column label="盘盈数量" align="center" prop="takingMore"/>
        <el-table-column label="盘亏数量" align="center" prop="takingLess"/>
      </el-table>

      <el-dialog title="选择 仓库"
                 :visible.sync="showSelHouse"
                 width="60%"
                 :before-close="handleClose"
                 :modal-append-to-body="false"
                 :append-to-body="true">
        <el-table :data="houseList">
          <el-table-column label="序号" align="center">
            <template slot-scope="scope">
              {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="仓库编号" align="center" prop="houseCode">
            <template slot-scope="scope">
              <el-button type="text" @click="confirmHouse(scope.row)">{{
                  scope.row.houseCode
                }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="仓库名称" align="center" prop="houseName"/>
          <el-table-column label="创建人" align="center" prop="createBy"/>
          <el-table-column label="创建时间" align="center" prop="createDate"/>
        </el-table>
        <pagination
          v-show="totalSize>0"
          :total="totalSize"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="selHouse"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelSelHouse">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog title="选择 商品类别"
                 :visible.sync="showSelType"
                 width="40%"
                 :before-close="handleClose"
                 :modal-append-to-body="false"
                 :append-to-body="true">
        <el-table :data="typeList">
          <el-table-column label="序号" align="center">
            <template slot-scope="scope">
              {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="typeName" align="center" prop="typeName">
            <template slot-scope="scope">
              <el-button type="text" @click="confirmType(scope.row)">{{
                  scope.row.typeName
                }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="totalSize>0"
          :total="totalSize"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="selType"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelSelType">取 消</el-button>
        </div>
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="submitForm">提 交</el-button>
        <el-button @click="closeAdd">关 闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  name: "AddStockTaking",
  props: ["updDataTo"],
  data() {
    return {
      showSelHouse: false,
      title: "添加 盘点单",
      dialogVisible: false,
      addShow: false,
      isUpd: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        takingCode: "",
        houseId: 0,
        houseName: "",
        typeId: 0,
        typeName: ""
      },
      totalDetail: 0,
      totalSize: 0,
      totalPrice: 0,
      houseList: [],
      typeList: [],
      showSelType: false,
      goodsList: [],
      stockTakingDetailList: [],
    };
  },
  watch: {
    updDataTo(newValue) {
      this.form = newValue;
      this.getStockTakingDetailList();
    }
  },
  methods: {
    confirmHouse(row) {
      this.form.houseName = row.houseName;
      this.form.houseId = row.id;
      this.queryParams.pageNum = 1;
      this.showSelHouse = false;
    },
    cancelSelHouse() {
      this.showSelHouse = false;
      this.queryParams.pageNum = 1;
    },
    selHouse() {
      request({
        url: "/store/warehouseInfo/list",
        params: this.queryParams,
        method: "get"
      }).then(res => {
        this.houseList = res.rows;
        this.totalSize = res.total;
        this.showSelHouse = true;
      })
    },
    confirmType(row) {
      this.form.typeName = row.typeName;
      this.form.typeId = row.id;
      this.queryParams.pageNum = 1;
      this.showSelType = false;
    },
    cancelSelType() {
      this.showSelType = false;
      this.queryParams.pageNum = 1;
    },
    //确认选择
    confirmSel() {
      if (!(this.form.houseId > 0)) {
        this.$notify({
          title: "警告",
          type: "warning",
          message: "请选择盘点仓库"
        });
        return;
      }
      request({
        url: "/store/warehouseGoods/takingList",
        params: {
          typeId: this.form.typeId,
          houseId: this.form.houseId
        },
        method: "get"
      }).then(res => {
        this.goodsList = res.rows;
        this.stockTakingDetailList = [];
        this.goodsList.forEach(item => {
          const detail = {
            goodsId: item.goodsId,
            houseId: item.houseId,
            proName: item.proName,
            realNum: item.stock,
            stock: item.stock,
            takingLess: 0,
            takingMore: 0,
            createBy: this.$store.getters.name
          }
          this.stockTakingDetailList.push(detail);
        });
      })
    },
    selType() {
      request({
        url: "/basedata/type/list",
        params: this.queryParams,
        method: "get"
      }).then(res => {
        this.typeList = res.rows;
        this.totalSize = res.total;
        this.showSelType = true;
      })
    },
    reset() {
      this.resetForm("form");
    },
    //修改总数量
    changeRealNum(row) {
      if (isNaN(row.realNum) || row.realNum <= 0) {
        this.$notify({
          title: '警告',
          message: '请输入正整数',
          type: 'warning',
        });
        row.realNum = row.stock; // 恢复为num
        return;
      }
      if (row.realNum >= row.stock) {
        row.takingMore = row.realNum - row.stock;
        row.takingLess = 0;
      } else {
        row.takingMore = 0;
        row.takingLess = row.stock - row.realNum;
      }
    },
    showAdd() {
      this.form = {
        cost: 0
      };
      this.isUpd = false;
      this.stockTakingDetailList = [];
      this.title = "添加 盘点单";
      this.form.takingCode = "";
      this.form.takingCode = "PD" + Date.now();
      this.addShow = true;
    },
    showUpd() {
      this.title = "开始盘点";
      // this.form = this.updData;
      this.isUpd = true;
      this.addShow = true;
      // this.getStockTakingDetailList();
    },
    getStockTakingDetailList() {
      this.stockTakingDetailList = [];
      request({
        url: "/store/StockTakingDetail/list",
        params: {
          takingId: this.form.id
        },
        method: "get"
      }).then(res => {
        this.stockTakingDetailList = res.rows;
        // this.addShow = true;
      });
    },
    closeAdd() {
      this.reset();
      this.isUpd = false;
      this.addShow = false;
    },
    handleClose(done) {
      done();
    },
    check() {
      if (this.form.takingCode == null || this.form.takingCode == '') {
        this.$notify({
          title: "警告",
          message: "盘点单编号不能为空！",
          type: "warning",
        })
        return false;
      }
      if (!(this.form.houseId > 0)) {
        this.$notify({
          title: "警告",
          message: "请选择仓库！",
          type: "warning",
        })
        return false;
      }
      if (!(this.stockTakingDetailList.length > 0)) {
        this.$notify({
          title: "警告",
          message: "请选择盘点商品！",
          type: "warning",
        })
        return false;
      }
      return true;
    },
    submitForm() {
      if (!this.check()) {
        return;
      }
      this.form.createBy = this.$store.getters.name;
      this.form.updateBy = this.$store.getters.name;
      this.stockTakingDetailList.forEach(item => {
        item.createBy = this.$store.getters.name
      });
      this.form.stockTakingDetails = this.stockTakingDetailList;
      if (!this.isUpd) {
        request({
          url: "/store/StockTaking/addStockTaking",
          data: this.form,
          method: "post",
        }).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess("新增成功");
            this.$emit("updList");
            this.addShow = false;
            this.reset();
          } else {
            this.$modal.msgError(res.msg);
          }
        }).catch(e => {
          this.$modal.msgError(e);
        })
      } else {
        this.form.status = 1;
        this.form.stockTakingDetails = this.stockTakingDetailList;
        this.form.modify = this.$store.getters.name;
        request({
          url: "/store/StockTaking/updStockTaking",
          data: this.form,
          method: "post",
        }).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess("修改成功");
            this.$emit("updList");
            this.addShow = false;
            this.reset();
          } else {
            this.$modal.msgError(res.msg);
          }
        }).catch(e => {
          this.$modal.msgError(e);
        })
      }
    },
    cancel() {
      this.showSelSaleBack = false;
      this.queryParams.pageNum = 1;
    }
  },
};
</script>
